$(function(){

  // isSubscribe(); //是否关注公众号
  
  // var pic = getQueryString("pic");
  // var id = getQueryString("id");
  // var type = getQueryString("type");

console.log(window.location.search)
/*
	var openId = localStorage.getItem("openId");
    if(openId == null || openId == ''){
      messageBox("获取个人信息失败");
      setTimeout(function(){
          loginFail('1'); // 一键登录
      },200);
      // return false;
    }
*/
  // var detail = getQueryString("detail");

  // var searchUrl =window.location.search;
  // var str = searchUrl.substr(1);
  // strs = str.split("&");  
  // var type = unescape(strs[1].split("=")[1]);;
  // var id = unescape(strs[0].split("=")[1]);;
  // var carName = unescape(strs[5].split("=")[1]);;


  var searchUrl =window.location.search;
  var str = decodeURI(searchUrl)
  strs = str.split("&");  
  console.log(strs)
  var id = strs[0].split('=')[1];
  var type = strs[1].split('=')[1];
  var carName=strs[5].split('=')[1];

  console.log(id)
  // var type = unescape(strs[1].split("=")[1]);;
  // var id = unescape(strs[0].split("=")[1]);;
  // var carName = unescape(strs[5].split("=")[1]);;
  // var searchUrl =window.location.search;
  // var searchData =searchUrl.split("=");
  // console.log(decodeURI(searchData[1]))
  // console.log(JSON.parse(decodeURI(searchData[1])))
  // var  A = JSON.parse(decodeURI(searchData[1]));
  // var type = A.type;
  // var id = A.id;
  // var carName = A.carName;
  
  console.log("type="+type+"=carName="+carName);
  console.log(id)
  var userName = localStorage.getItem("openId");
 
  if(type == '0'){
    var saleType = "亲子";
  }
  if(type == '1'){
    var saleType = "互联网";
  }
  if(type == '2'){
    var saleType = "创业";
  }
  if(type == '3'){
    var saleType = "职业培训";
  }
  if(type == '4'){
    var saleType = "兴趣培养";
  }
  if(type == '5'){
    var saleType = "运动&户外";
  }
  if(type == '6'){
    var saleType = "丽人";
  }
  if(type == '7'){
    var saleType = "交友聚会";
  }
  if(type == '8'){
    var saleType = "企业服务";
  }
  if(type == '9'){
    var saleType = "演出&展览";
  }
// 加载产品数据
$("#wrap>div").attr("data-id",id);
$.ajax({
  type: "GET",
  url: baseUrl + "/rest/activeMainController/list/"+userName,
  contentType:"application/json;charset=UTF-8",
  data: {
    id: id,
    fxjOut11:saleType,
    pageNumber:'1',
    pageSize:'10'
  },
  dataType: "json",
  success: function(data){
    if(data.ok){
       console.log(data);
      if(data.data.length){
        var str =data.data[0].fxjOut12;
        
        console.log(data)
        console.log(data.data[0].fxjOut08)
        $("#contact").append(data.data[0].fxjOut08);
        $("#banner").attr("src",baseUrl+"/" + data.data[0].fxjOut02); // banner
        $(".compangName").text(data.data[0].fxjOut03); // 车名
        $(".carName").text(data.data[0].fxjOut03); // 活动名字
        $(".carPrice-1").text(data.data[0].fxjOut06); // 活动费用
        $(".detail-address").text(data.data[0].fxjOut07); // 活动地点
        $(".startTime").text(data.data[0].fxjOut04); // 开始时间
        $(".endTime").text(data.data[0].fxjOut05); // 结束时间
        $(".giveTime").text(data.data[0].fxjOut14); // 活动整体时间         
        $("#carNo").val(data.data[0].carNo);
        $("#carCover").val(data.data[0].fxjOut02);
       
         var pic = data.data[0].fxjOut02 ;
        /*
		    var fxjOut15 =data.data[0].fxjOut15 + 1,
        InfoData={
		      id:id,
          fxjOut08:data.data[0].fxjOut08,
          fxjOut03:data.data[0].fxjOut03,
          fxjOut04:data.data[0].fxjOut04,
          fxjOut05:data.data[0].fxjOut05,
          fxjOut06:data.data[0].fxjOut06,
          fxjOut07:data.data[0].fxjOut07,
          fxjOut02:data.data[0].fxjOut02,
          fxjOut14:data.data[0].fxjOut14,
          fxjOut01:data.data[0].fxjOut01,
          fxjOut11:data.data[0].fxjOut11,
          fxjOut13:data.data[0].fxjOut13,
          fxjOut10:data.data[0].fxjOut10,
          fxjOut09:data.data[0].fxjOut09,
          fxjOut10:data.data[0].fxjOut10,
          fxjOut15:fxjOut15,
        }
      
        $.ajax({
          type: "PUT",
          url: baseUrl+"/rest/activeMainController/"+id,
          contentType: "application/json",
          data: JSON.stringify(InfoData),
          dataType: "json",
          success: function(data){
            if(data.ok){
              // console.log(data);
              $(".read").text(fxjOut15); 
            }else{
              console.log("数据加载失败...");
            }
          }
        });*/
        //活动人数
        $.ajax({
          type: "GET",
          url: baseUrl + "/rest/activeJoinController/list/"+userName,
          contentType:"application/json;charset=UTF-8",
          data: {
            fxjOut01: id,
            pageNumber:'1',
            pageSize:'2000'
          },
          dataType: "json",
          success: function(data){
            if(data.ok){
              // console.log(data);
              if(data.data.length){
                
                // $("#contact").append(a);
                $(".joinNumber").text(data.data.length+200); 

                
              }else{
                $(".joinNumber").text('200'); 
              }
            }else{
              console.log("数据加载失败...");
            }
          }
        
        });
       
      }else{
        console.log("暂无相关信息...")
      }
    }else{
      console.log("数据加载失败...");
    }
  }
});


  
  // 立即预约
    $("#yuyue").click(function (){
      $(".tel").css("display","block");
      $(".success").click(function(){
        var data = '';
        var name = $("#name").val();
        // var sex = $(':radio[name="sex"]:checked').val();
        var tel = $("#tel").val();
        var carName = getQueryString("carName");
        /*var searchUrl =window.location.href;
        var searchData =searchUrl.split("=");
        var  A =JSON.parse(decodeURI(searchData[1]));
        var picUrl = A.picUrl;*/
        // var smsCode = $("#code1").val();
        var searchUrl =window.location.search;
        var str = decodeURI(searchUrl)
        strs = str.split("&");  
        console.log(strs)
        var id = strs[0].split('=')[1];
        var startTime = strs[3].split('=')[1];
        var carName=strs[5].split('=')[1]; 
        var picUrl = strs[2].split('=')[1]; 
        var address = strs[4].split('=')[1]; 
        if(name == '' || name == null){
          messageBox('请输入姓名');
          return false;
        }
        
        if(tel == '' || tel == null){
          messageBox('请输入手机号');
          return false;
        }
       var userName = localStorage.getItem("openId");
console.log(address)
        /* var startTime = A.startTime;
        var searchUrl =window.location.href;
        var carName =A.carName;//获取需要的参数
        var address =A.address;*/
        /*
        $.ajax({
          type: "GET",
          url: baseUrl + "/rest/activeVipController/list/admin?",
          contentType:"application/json;charset=UTF-8",
          data: {
            pageNumber:'1',
            pageSize:'10',
            fxjOut02:userName,
          },
          dataType: "json",
          success: function(data){
            if(data.data == null || data.data ==''){
              messageBox("请先登录")
              setTimeout(function(){
                window.location = "../view/login.html";
              },1000);
            }
          }
        });
		*/
          console.log(carName)
          console.log(picUrl)
            data = {
              fxjOut16:startTime,//开始时间
              fxjOut13:picUrl,
              fxjOut04:userName,
              fxjOut01: id,
              fxjOut02:carName,//活动名字
              fxjOut06: name,//姓名
              fxjOut07: tel,//电话
              fxjOut08:'',//购买数量
              fxjOut10:'',//金额
              fxjOut11:'未使用',//使用情况
              fxjOut10:'',//活动开始时间
              // fxjOut14:sex,//性别
              fxjOut15:address,//地址
            }
          
        if(data){
          console.log(data);
          tijiao(data);
          
        } else{
          messageBox("");
        }
    });
    $(".cancel").click(function(){
      $(".tel").css("display","none");
    }); 
  });
  
  

  share();
//  putInfo()



});

// 提交预约
function tijiao(data){
  $.ajax({
    type: "POST",
    url: baseUrl + "/rest/activeJoinController",
    contentType:"application/json;charset=UTF-8",
    data:JSON.stringify(data),
    success: function(data){
      console.log(JSON.parse(data));
      let e = JSON.parse(data);
      var phone = $.trim($('#tel').val());
			if (phoneReg.test(phone)) {
        if(e.ok){
          messageBox("提交成功！")
          setTimeout(function(){
            window.location = "../index.html";
          },1000);
          return false;
        }else{
          messageBox(e.message)
        }
			}else{
				alert("请输入有效的手机号码")
        return false;
      }
     
    }
  });
}
function share(){
  console.log(shareTitle)
  console.log(shareDesc)
  //定义分享字段内容
  var shareTitle= '1000个精品玩具，豪华遥控赛车、芭比娃娃免费赠送、先到先得！' ;
  var shareDesc='1000个精品玩具，豪华遥控赛车、芭比娃娃免费赠送、先到先得！领取条件：新阳、霞阳附近常住群众孩子年龄0-16岁家长微信好友达200人以上愿意为本次活动宣传推广领取时间：8月19日 10：00~18：00领取地址：海沧区新景路新安西片173号3楼 潘多拉国际教育机构本次赠送玩具为豪华遥控赛车、芭比娃娃任选其一，每个孩子限领一份！活动真实有效，无需报任何课程均可赠送！';
  var shareImgUrl='http://www.zhaodui.com.cn/hdb/code/h5/static/images/2.png';
  // alert(url);
  // alert(encodeURIComponent(url));
  // var shareData = {
  //     contoUrl:encodeURIComponent(url)
  // }
  var shareData = {
      contoUrl:window.location.href
  }
  
  $.ajax({
      type: "POST",
      url: baseUrl+"/rest/tenPayController/getSignature/fxjpay",
      contentType: "application/json",
      data: JSON.stringify(shareData),
      dataType: "json",
      success: function (data) {
          var appId = 'wxf81777ac930f7ac3';
          var timestamp = data.timestamp;
          var nonceStr = data.nonceStr;
          var signature = data.signature; 
          var jsApiList =  ["updateAppMessageShareData", 'updateTimelineShareData', 'onMenuShareTimeline', 'onMenuShareAppMessage'];
          // var jsApiList = ['onMenuShareAppMessage','onMenuShareTimeline','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'];
          var testUrl = data.url;
          
          wx.config({
              debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
              appId: 'wxf81777ac930f7ac3', 
              timestamp:timestamp, // 必填，生成签名的时间戳
              nonceStr: nonceStr, // 必填，生成签名的随机串
              signature: signature,// 必填，签名，见附录1
              jsApiList: jsApiList // 必填，需要使用的J S接口列表，所有JS接口列表见附录2
          });
  
          wx.ready(function(){	
              wx.checkJsApi({
                  jsApiList :  ["updateAppMessageShareData", 'updateTimelineShareData', 'onMenuShareTimeline', 'onMenuShareAppMessage'],
                  success : function(res) {
                      // 以键值对的形式返回，可用的api值true，不可用为false
                      // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
                      // alert(res.checkResult);
                      // alert(res.errMsg);
                  }
              });
              // 分享朋友圈
              wx.onMenuShareTimeline({ 
                  title: shareTitle, 
                  desc: shareDesc,
                  link: testUrl,
                  imgUrl: shareImgUrl, 
                  success: function () {
                      console.log("设置回调成功1")
                  }
              });
              // 分享朋友
              wx.onMenuShareAppMessage({ 
                  title: shareTitle, 
                  desc: shareDesc,
                  link: testUrl,
                  imgUrl: shareImgUrl, 
                  success: function () {
                      console.log("设置回调成功2")
                  }
              });
              /*
              wx.updateAppMessageShareData({ 
                  title: shareTitle, // 分享标题
                  desc: shareDesc, // 分享描述
                  link: testUrl,
                  imgUrl: shareImgUrl, // 分享图标
                  success: function () {
                      //设置成功
                      console.log("设置分享给朋友回调成功")
                  },
                  fail:function(res){
                      console.log("设置分享给朋友回调失败，接口调用次数："+idNumber+",失败原因："+JSON.stringify(res))
                  }
              });
              
              wx.updateTimelineShareData({ 
                  title: shareTitle, // 分享标题
                  desc: shareDesc, // 分享描述
                  link: testUrl,
                  imgUrl: shareImgUrl, // 分享图标
                  success: function () {
                      //设置成功
                      console.log("设置分享到朋友圈回调成功,")
                  },
                  fail:function(res){
                          
                  }
              });

              */
              
          });
          wx.error(function(data){
              var resStr = "签名信息验证失败:"+JSON.stringify(data);		
              console.log(resStr)			  
          });	
          
      }
  });
  
}
// // 产品信息更新
// function putInfo(){
//   var id = getQueryString("id");
 
// }
